package com.hd.controller.activiti;
import com.hd.controller.base.BaseController;
import com.hd.entity.activiti.ActStartRight;
import com.hd.entity.activiti.ProcessType;
import com.hd.service.activiti.ActStartRightService;
import com.hd.service.activiti.ProcessTypeService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
* 流程类型
*/
@RequestMapping("processType")
@Controller
public class ProcessTypeController extends BaseController {
    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ActStartRightService actStartRightService;
    /**
     * 主页
     * @return
     */
    @RequestMapping("manager")
    public String manager() {
        return "jsp/activiti/processType/processTypeList";
    }
    /**
     * 列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping(value="/dataGrid",method=RequestMethod.POST)
    @ResponseBody
    public Object dataGrid(ProcessType processType, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object>condition=new HashMap<String,Object>();
        if(StringUtils.isNotNull(processType.getName())) {
        	condition.put("name", processType.getName());
        }
        pageInfo.setCondition(condition);
        processTypeService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    /**
     * 树
     * @return
     */
    @RequestMapping(value = "/tree",method=RequestMethod.POST)
    @ResponseBody
    public Object tree(ProcessType processType) {
        return processTypeService.selectTree(processType);
    }

    /**
     * 列表
     * #o-2
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid(ProcessType processType) {
        return processTypeService.selectTreeGrid(processType);
    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "jsp/activiti/processType/processTypeAdd";
    }
    /**
     * 查询所有
     * @return
     */
    @ResponseBody
    @RequestMapping("/findAll")
    public Object findALl() {
    	return processTypeService.findAll();
    }

    /**
     * 添加
     * @param processType
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(ProcessType processType) {
        processTypeService.save(processType);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable("id") String id) {
        processTypeService.delete(id);
        return renderSuccess("删除成功！");
    }
    /**
     * 作废
     * @param deploymentId
     * @return
     */
    @RequestMapping(value = "/cancel/{deploymentId}",method = RequestMethod.POST)
    @ResponseBody
    public Object cancel(@PathVariable("deploymentId") String deploymentId) {
        processTypeService.cancel(deploymentId);
        return renderSuccess("删除成功！");
    }
    /**
     * 编辑页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage/{id}")
	public String editPage(Model model, @PathVariable("id")String id) {
	 ProcessType processType = processTypeService.get(id);
        model.addAttribute("processType", processType);
        return "jsp/activiti/processType/processTypeEdit";
    }

    /**
     * 查看页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/viewPage/{id}")
    public String viewPage(Model model, @PathVariable("id") String id) {
        ProcessType processType = processTypeService.get(id);
        model.addAttribute("processType", processType);
        return "jsp/activiti/processType/processTypeView";
    }
    /**
     * 编辑
     * @param processType
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(ProcessType processType) {
        processTypeService.update(processType);
        return renderSuccess("编辑成功！");
    }

    /**
     * 跳转到设置流程发起页面
     * @param procdefid
     * @return
     */
    @RequestMapping(value = "/startRight/{procdefid}")
    public String startRight(@PathVariable("procdefid")String procdefid,Model model) {
    	model.addAttribute("procdefid", procdefid);
    	ActStartRight actStartRight = actStartRightService.getByFK(procdefid);
    	model.addAttribute("actStartRight", actStartRight);
    	return "jsp/activiti/processType/startRight";
    }
    /**
     * 保存发起权限
     * @return
     */
    @RequestMapping(value = "/saveStartRight",method = RequestMethod.POST)
    @ResponseBody
    public Object saveStartRight(ActStartRight actStartRight) {
    	if(actStartRight.getId()!=null) {
    		actStartRightService.update(actStartRight);
    	}else {
    		actStartRightService.save(actStartRight);
    	}
    	return renderSuccess("设置发起权限成功！");
    }
    
    /**
     * 跳转到流程类型选择页面
     * @param processTypeIds
     * @param model
     * @return
     */
    @RequestMapping("/toSelect")
    public String toSelect(String processTypeIds,Boolean flag,Model model) {
    	model.addAttribute("processTypeIds", processTypeIds);
    	model.addAttribute("flag", flag);
    	return "jsp/activiti/processType/processTypeSelect";
    }
}
